/*************************************************************************
*
*	simulate_battery.c
*
*  Copyright 2007,2008,2009 Steve Stancliff
*
*  Part of the TAOSF SSA system.
*
*  This module provides functions for simulating boat status.
*
*
**************************************************************************
*
*  version 1.1.1, 2/23/09, SBS
*   - add setting of timestamp
*
*  version 1.1, 2/2/09, SBS
*   - battery moved out of basic eng telem into an ENG_DAT struct
*
*  version 1.0.9, 2/1/09, SBS
*   - rename functions to SimBattery_
*   - remove deltatime parameter from _Update
*   - rename _Update --> _GetStatus
*   - cleanup
*
*  version 1.0.8, 10/17/08, SBS
*   - broken out from simulate_boat
*
*  version 1.0.7, 10/4/08, SBS
*   - documentation
*
*  version 1.0.6, 8/27/08, SBS
*   - use status_SetTimeString()
*   - add random offset to boat start positions
*
*  version 1.0.5, 5/30/08, SBS
*    - move conversions (lat, long, deg, rad, etc) into conversions.h
*    - add conditionals for simulating buoys
*    - cleanup
*
*  version 1.0.4, 5/29/08, SBS
*    - add UpdateBoatSpeed()
*    - add status "direction" = net motion direction
*    - use direction instead of heading in positionfromheadingspeed
*    - move InitBoat here from simulate_engineering
*
*  version 1.0.3, 5/18/08, SBS
*    - add boat rotating into wind during free drift
*
*  version 1.0.2, 5/17/08, SBS
*    - modify drift to include both wind and water 
*    - add turn rate to heading changes
*
*  version 1.0.1, 5/12/08, SBS
*    - add drift
*
*  version 1.0, 1/13/08, SBS
*    - split from simulate_engineering.c
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/
/**
*  @file 
*  @ingroup plat_eng_simulate
*  @brief
*  This module provides functions for simulating boat status.
*  <hr>
**/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#include "waypoints.h"
#include "platform_types.h"
#include "platform_modes.h"
#include "status.h"

#ifdef DEBUG
#include "logfile.h"
#endif

#include "simulate_battery.h"


#define INITBATT 10.0
#define MINBATT 10.0
#define MAXBATT 250.0
#define BATTINC 0.1


/********************************************************
*
* SimBattery_Init()
*/
/**
* @brief
* Set initial battery voltage
*
********************************************************/
void SimBattery_Init(
			STATUS *s)
{
	#ifdef DEBUG
	logfile_LogLevelPrint(LOGLEVEL_INFO,"SimBattery_Init()");
	#endif
	
	s->dMainBattery_V.dValue = INITBATT;
	strncpy(s->dMainBattery_V.sUnits,"V",sizeof(s->dMainBattery_V.sUnits));

	return;
}


/********************************************************
*
* SimBattery_GetStatus()
*/
/**
* @brief
* Simulates changes in boat battery voltage
*
********************************************************/
void SimBattery_GetStatus(
			STATUS *s)
{
	static double dBattInc = BATTINC;	

	#ifdef DEBUG
	logfile_LogLevelPrint(LOGLEVEL_INFO,"SimBattery_GetStatus()");
	#endif

	s->dMainBattery_V.dValue += dBattInc;

	if (s->dMainBattery_V.dValue > MAXBATT) {
		
		dBattInc = -dBattInc;
		s->dMainBattery_V.dValue = MAXBATT;
		
	}else if (s->dMainBattery_V.dValue < MINBATT) {
		
		dBattInc = -dBattInc;
		s->dMainBattery_V.dValue = MINBATT;
	
	}	
	
	snprintf(s->dMainBattery_V.sTime
			,sizeof(s->dMainBattery_V.sTime)
			,"%s",s->sTime);
	
	return;
}

